package com.freesky.quartz.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.freesky.biz.icbc.mapper.IntegralExchangeMapper;
import com.freesky.biz.icbc.model.IcbcPointQryResponseV1;
import com.freesky.biz.icbc.service.IcbcPointService;
import com.freesky.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 积分查询任务
 */
@Component("pointQueryTask")
public class IcbcPointQueryTask {

    private static final Logger logger = LoggerFactory.getLogger(IcbcPointQueryTask.class);

    @Autowired
    private IntegralExchangeMapper integralExchangeMapper;

    @Autowired
    private IcbcPointService icbcPointService;

    public void taskProcess() {
        logger.info("icbcPointQueryTask task start");
        List<String> integralList = integralExchangeMapper.selectIntegralQueryList();
        for(String mobile : integralList){
            AjaxResult ajaxResult = icbcPointService.pointQuery("0001",mobile);
            if(ajaxResult.isSuccess()){
                String integral = "0";
                IcbcPointQryResponseV1 icbcPointQryResponseV1 = (IcbcPointQryResponseV1)ajaxResult.get("data");
                if("00".equalsIgnoreCase(icbcPointQryResponseV1.getRetCode())) {
                    JSONArray jsonArray = JSONArray.parseArray(icbcPointQryResponseV1.getBody());
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String qyId = jsonObject.getString("qyid").trim();
                        if ("01".equalsIgnoreCase(qyId)) {
                            integral = jsonObject.getString("maxamt").trim();
                        }
                    }
                    integralExchangeMapper.updateIntegralQuery(integral, mobile,"成功");
                } else {
                    integralExchangeMapper.updateIntegralQuery("0",mobile, icbcPointQryResponseV1.getRetMsg());
                }
            } else {
                integralExchangeMapper.updateIntegralQuery("0",mobile, ajaxResult.getMsg());
            }
        }
    }
}
